# 数据层 充值退费相关信息加载
import datetime
# 充值信息
class Recharge:
    def __init__(self,rec_no,rec_money,rec_date,rec_cus_no):
        self.no=rec_no          # 1充值信息号
        self.money=rec_money    # 2充值金额
        self.date=rec_date      # 3充值日期
        self.cus_no=rec_cus_no  # 4充值顾客号

    def toString(self):
        return "{} {} {} {}".format(self.no,self.money,self.date,self.cus_no)


# 退费信息
class Refund:
    def __init__(self,ref_no,ref_wai_no,ref_money,ref_date,ref_orfo_no,ref_cus_no):
        self.no=ref_no              # 1退费信息号
        self.wai_no=ref_wai_no      # 2处理退费的客服号
        self.money=ref_money        # 3退费金额
        self.date=ref_date          # 4退费日期
        self.orfo_no=ref_orfo_no    # 5退费的订单号
        self.cus_no=ref_cus_no      # 6退费的顾客号

    def toString(self):
        return "{} {} {} {} {}".format(self.no,self.wai_no,self.money,self.date,self.orfo_no)

## 充值信息的相关操作
# 新增充值信息
def addRecharge(conn,cur,customer,money):
    # 获取自增1的订单号
    sqlText = 'select * from recharge;'
    cur.execute(sqlText)
    result = cur.fetchall()
    no = len(result) + 1
    no_str = str(no).zfill(5)
    # 获取当前时间
    curTime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    # print(curTime)
    # 插入数据
    sqlText = "insert into recharge values ('{}',{},'{}','{}');".format(no_str,money,curTime,customer.no)
    cur.execute(sqlText)
    conn.commit()

# 删，不允许删除充值信息

# 改，充值信息不允许被更改

# 查，根据顾客号查找充值信息
def selRechargeByCus_no(cur,cus_no):
    sqlText = 'select * from recharge where rec_cus_no="{}";'.format(cus_no)
    cur.execute(sqlText)
    return cur.fetchall()

# 查，根据日期查找充值信息，year
def selRechargeByDateYear(cur,year):
    sqlText = "select * from recharge where year(rec_date)='{}';".format(year)
    cur.execute(sqlText)
    return cur.fetchall()

# 查，根据日期查找充值信息，year、month
def selRechargeByDateYearMonth(cur,year,month):
    sqlText = "select * from recharge where year(rec_date)='{}'and month(orfo_date)='{}';".format(year,month)
    cur.execute(sqlText)
    return cur.fetchall()

# 查，根据日期查找充值信息，year、month、day
def selRechargeByDateYearMonthDay(cur,year,month,day):
    sqlText = "select * from recharge where year(rec_date)='{}'and month(orfo_date)='{}' and day(orfo_date)='{}';".format(year,month,day)
    cur.execute(sqlText)
    return cur.fetchall()



## 退费信息的相关操作
# 新增退费信息
def addRefund(conn,cur,waiter,orderform):
    # 获取自增1的订单号
    sqlText = 'select * from recharge;'
    cur.execute(sqlText)
    result = cur.fetchall()
    no = len(result) + 1
    no_str = str(no).zfill(5)
    # 获取当前时间
    curTime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    # print(curTime)
    # 插入数据
    sqlText = "insert into refund values ('{}','{}',{},'{}','{}','{}');".format(no_str, waiter.no, orderform.total,curTime,orderform.no,orderform.cus_no)
    cur.execute(sqlText)
    conn.commit()

# 删，不允许删除退费信息

# 改，不允许更改退费信息

# 查，根据客服员号查找退费信息
def selRefundByWai_no(cur,wai_no):
    sqlText = 'select * from refund where ref_wai_no="{}";'.format(wai_no)
    cur.execute(sqlText)
    return cur.fetchall()

# 查，根据顾客号查找退费信息
def selRefundByCus_no(cur,cus_no):
    sqlText = 'select * from refund where ref_cus_no="{}";'.format(cus_no)
    cur.execute(sqlText)
    return cur.fetchall()

# 查，根据时间查找退费信息，Year
def selRefundByDateYear(cur,year):
    sqlText = "select * from refund where year(ref_date)='{}';".format(year)
    cur.execute(sqlText)
    return cur.fetchall()

# 查，根据时间查找退费信息，Year、Month
def selRefundByDateYearMonth(cur,year,month):
    sqlText = "select * from refund where year(ref_date)='{}'and month(ref_date)='{}';".format(year, month)
    cur.execute(sqlText)
    return cur.fetchall()

# 查，根据时间查找退费信息，Year、Month、Day
def selRefundByDateYearMonthDay(cur,year,month,day):
    sqlText = "select * from refund where year(ref_date)='{}'and month(ref_date)='{}' and day(ref_date)='{}';".format(year, month, day)
    cur.execute(sqlText)
    return cur.fetchall()